function  formatobj(params) {
    let str="";
    for(let key in params){
        str+=key+"="+params[key]+"&";
    }
    str=str.substring(0,str.length-1);
    return str;
}

const http={
    get:function(url,data,callback){
        let xhr=new XMLHttpRequest();
        xhr.open('get',url+"?"+formatobj(data));
        xhr.send();
        xhr.onreadystatechange=function() {
            if(xhr.readyState==4 && xhr.status==200){
                callback(xhr.responseText);
            }
        }
    },

    post:function(url,data,callback) {
        let xhr=new XMLHttpRequest();
        xhr.open('post',url);
        // 模仿form表单
        // xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
        // xhr.send(formatobj(data));
        // json方式 
        xhr.setRequestHeader('Content-type','application/json');
        xhr.send(JSON.stringify(data));
        xhr.onreadystatechange=function(){
            if(xhr.readyState==4 && xhr.status==200){
                callback(xhr.responseText);
            }
        }
    },

    ajax:function(obj){
        if(obj.type=="get"){
            this.get(obj.url,obj.data,obj.sucess);
        }
        else if(obj.type=="post"){
            this.post(obj.url,obj.data,obj.sucess);
        }
    }
}
window.http=http;